<%@ page import="java.sql.ResultSet" %>
<%@ page import="util.JDBC" %>
<%@ page import="java.util.List" %>
<%@ page import="entity.Course" %>
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: 10799
  Date: 2018/12/7
  Time: 22:14
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>选课</title>
    <link rel="stylesheet" type="text/css" href="statics/css/initialition.css">
    <link rel="stylesheet" type="text/css" href="statics/css/selectCourse.css">
</head>
<body>
<div id="cqr">
    <%@ include file="header.jsp"%>
    <table>
        <caption>课程列表</caption>
        <tbody>
            <tr>
                <td>课程编号</td>
                <td>课程名</td>
                <td>任课老师</td>
                <td>学分</td>
                <td>选课人数</td>
                <td>操作</td>
            </tr>
            <%
                String userId="";
                try{
                    userId=session.getAttribute("userId").toString();
                }catch (Exception ex){//没有userId则跳转
                    response.sendRedirect("login.jsp");
                }
                JDBC.connectionTest();
                String sql="select * from course";
                ResultSet rs= JDBC.executeQuery(sql);
                List<Course> courses=new ArrayList<Course>();
                try{
                    while (rs.next()){
                        Course course=new Course();
                        course.setId(rs.getInt("id"));
                        course.setCourseName(rs.getString("courseName"));
                        course.setTecher(rs.getString("teacher"));
                        course.setCredit(rs.getDouble("credit"));
                        course.setStudent_count(rs.getInt("student_count"));
                        courses.add(course);
                    }
                }catch (Exception ex){
                    ex.printStackTrace();
                }
            %>
            <%
                for (Course course:courses){
            %>
            <tr>
                <td><%=course.getId()%></td>
                <td><%=course.getCourseName()%></td>
                <td><%=course.getTecher()%></td>
                <td><%=course.getCredit()%></td>
                <td><%=course.getStudent_count()%></td>
                <%
                    int count=0;
                    sql="select count(*) as count from student_course where studentId='"+userId+"' and courseId='"+course.getId()+"'";
                    System.out.println(sql);
                    rs=JDBC.executeQuery(sql);
                    while (rs.next()){
                        count=rs.getInt("count");
                    }
                %>
                <%
                    if(count==0){
                %>
                <td><a v-on:click="selectCourse(<%=course.getId()%>)"  style="background: royalblue">选课</a></td>
                <%
                    }else{
                %>
                <td><a v-on:click="abandonCourse(<%=course.getId()%>)"  style="background: darkgray">退选</a></td>
                <%
                    }
                %>
            </tr>
            <%
                }
            %>
        </tbody>
    </table>
</div>
<script src="statics/js/jquery-3.3.1.min.js"></script>
<script src="statics/js/vue.js"></script>
<script src="statics/js/selectCourse.js"></script>
</body>
</html>
